﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;

namespace JKF.Substructure
{
    public class Pager
    {
        public int RecordCount { get; set; }

        public Pager(int pagesize)
        {
            this._pageSize = pagesize;
        }

        private int _pageSize;

        /// <summary>
        /// 剪切数据源
        /// </summary>
        /// <param name="scource">源</param>
        /// <param name="pageindex">当前页 从0开始</param>
        /// <returns></returns>
        public IQueryable Cut(IQueryable scource, int pageindex)
        {
            RecordCount = scource.Count();
            var q = scource.Skip(pageindex * _pageSize).Take(_pageSize);
            return q;
        }

        public int PageCount
        {
            get
            {
                if (this.RecordCount == 0)
                {
                    return 0;
                }
                if (this.RecordCount % this._pageSize == 0)
                {
                    return this.RecordCount / this._pageSize;
                }
                else
                {
                    return this.RecordCount / this._pageSize + 1;
                }
            }
        }
    }
}
